import os
import shutil

dir = "E:/sunx/碰撞场景分类/test1"         #获取pic目录
subdir = os.listdir(dir)  #遍历当前目录下所有文件
# print(subdir)
for name_0 in subdir:
    path = os.path.join(dir,name_0) # 合成一级目录
    if os.path.isdir(path):
        dir1 = os.listdir(path) # 一级目录名列表
        for name_1 in dir1:
            print(name_1[::-1][:7])
            name1_ = name_1[::-1][:7]
            path_1 = os.path.join(path, name_1) # 合成二级目录
            # print(path_1)
            if os.path.isdir(path):
                dir2 = os.listdir(path_1) # 二级目录名列表
                for name_2 in dir2:
                    path_2 = os.path.join(path, name_1)  # 合成三级目录
                    newname = name1_+name_2
                    print(newname)
                    route1 = os.path.join(path_2, newname)
                    route2 = 'E:/sunx/碰撞场景分类/klicen-s_pic'
                    os.rename(os.path.join(path_2, name_2), os.path.join(path_2, newname))
                    try:
                        shutil.move(route1,route2)
                    except shutil.Error:
                        continue

